﻿using Microsoft.Extensions.DependencyInjection;
using SSHTool.Util;
using System;

namespace SSHTool.Service
{
    public class PageService
    {
        private static readonly string NameSpaceString = typeof(PageService).Assembly.GetName().Name;

        private readonly IServiceProvider serviceProvider;
        public PageService(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        public object CreatePage(string className)
        {
            try
            {
                var type = Type.GetType($"{NameSpaceString}.View.{className}");
                //return type == null ? null : Activator.CreateInstance(type);

                return serviceProvider.GetService(type);
            }
            catch (Exception e)
            {
                LogUtil.Instance.Error(e);
                return null;
            }
        }

        public TService GetService<TService>()
        {
            return serviceProvider.GetService<TService>();
        }
    }
}
